package com.ohuang.wallpapermanager.compose

import android.content.Context
import android.os.SystemClock
import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


inline fun <reified T> T.toMutableState(): MutableState<T> {
    return mutableStateOf(this as T)
}

inline fun <reified T> MutableState<T>.update(block: (T) -> T) {
    this.value = block(this.value)
}

@Composable
fun getContext(): Context {
    return LocalContext.current
}

@Composable
fun Number.dp2sp(): TextUnit {
    val current = LocalContext.current
    return dp2sp(current).sp
}


fun Modifier.onClick(enabled: Boolean = true, onClick: () -> Unit) = composed {
    this.clickable(
        enabled = enabled,
        onClick = onClick,
        indication = null,
        interactionSource = remember { MutableInteractionSource() })
}


fun Modifier.onClickSafe(enabled: Boolean = true, time: Long = 500, onClick: () -> Unit) =
    composed {
        val lastTime = remember {
            mutableStateOf(0L)
        }
        return@composed this.onClick(enabled = enabled, onClick = {
            if (SystemClock.uptimeMillis() - lastTime.value > time) {
                onClick()
                lastTime.value = SystemClock.uptimeMillis()
            }
        })
    }


fun Modifier.clickableSafe(enabled: Boolean = true, time: Long = 500, onClick: () -> Unit) =
    composed {
        val lastTime = remember {
            mutableStateOf(0L)
        }
        return@composed this.clickable(enabled = enabled, onClick = {
            if (SystemClock.uptimeMillis() - lastTime.value > time) {
                onClick()
                lastTime.value = SystemClock.uptimeMillis()
            }
        })
    }


fun Modifier.paddingWithStatusBar() =
    composed {
        Modifier.padding(
            top = LocalContext.current.getStatusBarHeight().px2dp(LocalContext.current).dp
        )
    }

@Composable
fun getString(@StringRes id: Int): String {
    return LocalContext.current.getString(id)
}


fun Context.getStatusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (resourceId > 0) {
        resources.getDimensionPixelSize(resourceId)
    } else {
        0
    }
}